//
//  VehicleDistributionView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/15.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "VehicleDistributionView.h"
#import "OperationPiechartCell.h"
#import "HeatMapViewCell.h"
#import "DistributionMapCell.h"
@interface VehicleDistributionView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *distributionTabelView;
@property (nonatomic,strong)NSArray *carNameStatis;
@property (nonatomic,strong)NSArray *carTypeStatis;
@property (nonatomic,strong)NSArray *mapPoint;
@property (nonatomic,assign)BOOL allowLoad;
@property (nonatomic,strong)LoadNetView *loadNetView;
@end
static NSString *HeatMapViewCellID = @"HeatMapViewCellID";
static NSString *DistributionMapCellID = @"DistributionMapCellID";
static NSString *OperationPiechartCellID = @"OperationPiechartCellID";

static NSString *distributionCellID1 = @"distributionCellID1";
static NSString *distributionCellID2 = @"distributionCellID2";

@implementation VehicleDistributionView
- (LoadNetView *)loadNetView
{
    if (!_loadNetView) {
        _loadNetView = [LoadNetView new];
        [self addSubview:_loadNetView];
        [_loadNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        _loadNetView.hidden = NO;
    }
    return _loadNetView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
        _allowLoad = NO;
        _needRefresh = YES;
      
    }
    return self;
}
- (void)setUpUI
{
    _distributionTabelView = [UITableView new];
    _distributionTabelView.dataSource = self;
    _distributionTabelView.delegate = self;
    _distributionTabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [_distributionTabelView registerClass:[OperationPiechartCell class] forCellReuseIdentifier:distributionCellID1];
    [_distributionTabelView registerClass:[OperationPiechartCell class] forCellReuseIdentifier:distributionCellID2];
    [_distributionTabelView registerClass:[HeatMapViewCell class] forCellReuseIdentifier:HeatMapViewCellID];

    [_distributionTabelView registerClass:[DistributionMapCell class] forCellReuseIdentifier:DistributionMapCellID];
    [_distributionTabelView registerClass:[OperationPiechartCell class] forCellReuseIdentifier:OperationPiechartCellID];
    _distributionTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _distributionTabelView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self addSubview:self.distributionTabelView];
    [self.distributionTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-TabBarHeight-10);
    }];
//    [self loadDataWithParam:nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_allowLoad) {
        return 3;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    switch (indexPath.row) {
//        case 0:
//        {
//            DistributionMapCell *distributionCell = [DistributionMapCell new];
//            distributionCell.selectionStyle = UITableViewCellSelectionStyleNone;
//            if (self.mapPoint) {
//                [distributionCell loadDistributionMapWithArr:self.mapPoint];
//            }
//            return distributionCell;
//        }
//            break;
//        case 1:
//        {
//            OperationPiechartCell *nameCell =  [OperationPiechartCell new];
//            if (self.carNameStatis) {
//                [nameCell laodUIWithDataArr:self.carNameStatis];
//            }
//            nameCell.title = @"车辆名称";
//            nameCell.selectionStyle = UITableViewCellSelectionStyleNone;
//            return nameCell;
//        }
//            break;
//        case 2:
//        {
//            OperationPiechartCell *typeCell = [OperationPiechartCell new];
//            if (self.carTypeStatis) {
//                [typeCell laodUIWithDataArr:self.carTypeStatis];
//            }
//            typeCell.title = @"车辆类型";
//            typeCell.selectionStyle = UITableViewCellSelectionStyleNone;
//            return typeCell;
//        }
//            break;
//        case 3:
//        {
//            HeatMapViewCell *heatMapCell = [HeatMapViewCell new];
//            heatMapCell.selectionStyle = UITableViewCellSelectionStyleNone;
//            if (self.mapPoint.count) {
//                [heatMapCell loadMapWithArr:self.mapPoint];
//            }
//            return heatMapCell;
//        }
//            break;
//        default:
//            return nil;
//            break;
//    }
    switch (indexPath.row) {
//        case 0:
//        {
//            DistributionMapCell *distributionCell = [DistributionMapCell new];
//            distributionCell.selectionStyle = UITableViewCellSelectionStyleNone;
//            if (self.mapPoint) {
//                [distributionCell loadDistributionMapWithArr:self.mapPoint];
//            }
//            return distributionCell;
//        }
//            break;
        case 0:
        {
            OperationPiechartCell *nameCell =  [OperationPiechartCell new];
            if (self.carNameStatis) {
                [nameCell laodUIWithDataArr:self.carNameStatis];
            }
            nameCell.title = @"车辆名称";
            nameCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return nameCell;
        }
            break;
        case 1:
        {
            OperationPiechartCell *typeCell = [OperationPiechartCell new];
            if (self.carTypeStatis) {
                [typeCell laodUIWithDataArr:self.carTypeStatis];
            }
            typeCell.title = @"车辆类型";
            typeCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return typeCell;
        }
            break;
        case 2:
        {
            HeatMapViewCell *heatMapCell = [HeatMapViewCell new];
            heatMapCell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (self.mapPoint.count) {
                [heatMapCell loadMapWithArr:self.mapPoint];
            }
            return heatMapCell;
        }
            break;
            default:
            return nil;
            break;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    switch (indexPath.row) {
//        case 0:
//            return 360;
//            break;
//        case 1:
//            return 299;
//            break;
//        case 2:
//            return 299;
//            break;
//        case 3:
//            return 360;
//            break;
//        default:
//            return 299;
//            break;
//    }
//
    switch (indexPath.row) {
//        case 0:
//            return 360;
//            break;
        case 0:
            return 299;
            break;
        case 1:
            return 299;
            break;
        case 2:
            return 360;
            break;
        default:
            return 299;
            break;
    }
}


- (void)loadNewData
{
   [self.loadNetView start];
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_CarDistributionByDay method:@"POST" parameters:_param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
        NSLog(@"%@",responseObject);
        if ([responseObject[@"state"] integerValue] == 0) {
            NSDictionary *dic = responseObject[@"obj"];
            self.carNameStatis = dic[@"carNameStatis"];
            self.carTypeStatis = dic[@"carTypeStatis"];
            self.mapPoint = dic[@"mapPoint"];
            _allowLoad = YES;
            [_distributionTabelView reloadData];
           
        }
         [_distributionTabelView.mj_header endRefreshing];
    } failure:^(NSError * _Nonnull error) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
        [_distributionTabelView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
    

}
- (void)loadDataBackGroundWithParam:(NSDictionary *)param
{
    
    if (_needRefresh) {
        [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_CarDistributionByDay method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(id  _Nullable responseObject) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            if ([responseObject[@"state"] integerValue] == 0) {
                NSDictionary *dic = responseObject[@"obj"];
                self.carNameStatis = dic[@"carNameStatis"];
                self.carTypeStatis = dic[@"carTypeStatis"];
                self.mapPoint = dic[@"mapPoint"];
                _allowLoad = YES;
                _needRefresh = NO;
                [_distributionTabelView reloadData];
            }
             [_distributionTabelView.mj_header endRefreshing];
        } failure:^(NSError * _Nonnull error) {
             [_distributionTabelView.mj_header endRefreshing];
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
            NSLog(@"%@",error);
        }];
    }
}
- (void)loadDataWithParam:(NSDictionary *)param
{
    
    if (_needRefresh) {
        [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_CarDistributionByDay method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(id  _Nullable responseObject) {
             [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            NSLog(@"%@",responseObject);
            if ([responseObject[@"state"] integerValue] == 0) {
                NSDictionary *dic = responseObject[@"obj"];
                self.carNameStatis = dic[@"carNameStatis"];
                self.carTypeStatis = dic[@"carTypeStatis"];
                self.mapPoint = dic[@"mapPoint"];
                _allowLoad = YES;
                _needRefresh = NO;
                [_distributionTabelView reloadData];
            }
             [_distributionTabelView.mj_header endRefreshing];
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
             [_distributionTabelView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
}
@end
